﻿Public Class FrmMain : Inherits Library.MainForm

	Dim Homepage As Homepage

	Sub New()
		InitializeComponent()
		Image.Image = My.Resources.calendar.Resize(Image.Width, Image.Height)
		TabStrip.Visible = False
		HelpMenu = Me.HelpStrip
		HelpMenu.Renderer = New FirefoxRenderer
	End Sub

	Public Overrides Sub Connect()
		Dim SSD As Boolean
		For Each IP In My.Computer.Network.Addresses
			If IP Like "172.25.61.*" Then SSD = True
			If IP Like "172.23.6.*" Then SSD = True
			If IP Like "172.26.26.*" Then SSD = True
			If IP Like "172.26.28.*" Then SSD = True
			If IP Like "172.26.29.*" Then SSD = True
			If IP Like "172.26.30.*" Then SSD = True
		Next
		If SSD Then
			OpenConnection("192.168.20.3", "IZIN", "appl", "SSD", "izin")
		Else
			OpenConnection("172.25.10.9", "pizin", "pizinadmin", "relayer", "izin")
			ExecSQL("update personel set yetki = 'izin.opr(5)' where kod = 382")
		End If
	End Sub

	Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
		If keyData = Keys.Escape Then
			If ActiveControl Is CmbFind Then
				SendKeys.Send("{TAB}")
			Else
				CloseTab()
				Return True
			End If
		End If
		Return MyBase.ProcessCmdKey(msg, keyData)
	End Function

	Overrides Sub CloseTab()
		If TabStrip.SelectedTab.Control Is Homepage Or TabStrip.Tabs.Count = 1 Then Close() : Exit Sub
		MyBase.CloseTab()
	End Sub

	Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
		MyBase.OnLoad(e)
		Connect()
		FrmLogin.AppKey = "izin"
		If FrmLogin.ShowDialog = Windows.Forms.DialogResult.Cancel Then Close() : End
		Service.Log("login", 118, Kullanici.Kod)
		If Not Kullanici.Operator And Not Kullanici.Yonetici Then
			CmbFind.Visible = False
			Open(Kullanici.Kod, True)
			TabStrip.Visible = True
			Exit Sub
		Else
			CmbFind.Parent = Me
			CmbFind.Location = New Point(ClientRectangle.Width - CmbFind.Width - 6, 10)
			CmbFind.Anchor = AnchorStyles.Right Or AnchorStyles.Top
			CmbFind.BringToFront()
		End If
		Homepage = New Homepage
		Controls.Add(Homepage)
		Homepage.Dock = DockStyle.Fill
		Homepage.BringToFront()
		TabStrip.AddTab("Personel listesi", My.Resources.calendar, Homepage, True)
		Homepage.Populate()
		Homepage.Focus()
	End Sub

	Sub Open(ByVal Kod As Integer, Optional ByVal Locked As Boolean = False)
		For Each Tab In TabStrip.Tabs.Where(Function(x) TypeOf x.Control Is CtlPersonel)
			Dim Ctl As CtlPersonel = Tab.Control
			If Ctl.Personel = Kod Then Tab.Activate() : Exit Sub
		Next
		Dim Form As New CtlPersonel(Kod)
		Controls.Add(Form)
		Form.Dock = DockStyle.Fill
		Form.BringToFront()
		TabStrip.AddTab(Form.Text, Form.Icon, Form, Locked)
		Form.Focus()
		Service.Log("open", Kod, Kullanici.Kod)
	End Sub

	Sub ShowMap()
		Dim Form As New Map
		Controls.Add(Form)
		Form.Dock = DockStyle.Fill
		Form.BringToFront()
		TabStrip.AddTab("İzin Çizelgesi", My.Resources.chart, Form, False)
	End Sub

	Sub ShowReports()
		Dim Form As New Reports
		Controls.Add(Form)
		Form.Dock = DockStyle.Fill
		Form.BringToFront()
		TabStrip.AddTab("Raporlar", My.Resources.chart, Form, False)
		Form.NewReport()
	End Sub

	Private Sub CmbFind_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CmbFind.SelectedValueChanged
		If CmbFind.SelectedValue Is Nothing Then Exit Sub
		If IsDBNull(CmbFind.SelectedValue) Then Exit Sub
		If CmbFind.SelectedValue = "" Then Exit Sub
		Homepage.Open(CmbFind.SelectedValue)
		CmbFind.SelectedIndex = -1
	End Sub

	Private Sub MnuAbout_Click(sender As System.Object, e As System.EventArgs) Handles MnuAbout.Click
		Dim Form As New AboutForm
		Form.ShowDialog()
	End Sub

End Class

Class ImageCombo : Inherits ComboBox

	Sub New()
		MyBase.New()
		'DrawMode = Windows.Forms.DrawMode.OwnerDrawVariable
	End Sub

	<System.Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True)> _
	Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, ByVal lclassName As String, ByVal windowTitle As String) As IntPtr

	End Function
	<System.Runtime.InteropServices.DllImport("user32.dll")> _
	Public Shared Function MoveWindow(ByVal hWnd As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Boolean) As Boolean

	End Function


	Dim TextHandle As IntPtr

	Protected Overrides Sub OnHandleCreated(ByVal e As System.EventArgs)
		MyBase.OnHandleCreated(e)
		TextHandle = FindWindowEx(Me.Handle, IntPtr.Zero, "EDIT", "")
		MoveWindow(TextHandle, 22, 4, Width - 42, Height - 8, True)
	End Sub

	Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
		Static B As Bitmap = Nothing
		MyBase.WndProc(m)
		If m.Msg = 15 Then
			Dim R = ClientRectangle
			Dim G = Graphics.FromHwnd(Handle)
			R.Inflate(-2, -2)
			R.Width -= 20
			G.FillRectangle(Brushes.White, R)
			R.Width = R.Height
			R.Inflate(-1, -1)
			G.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
			If B Is Nothing Then B = My.Resources.open : B.RotateFlip(RotateFlipType.RotateNoneFlipX)
			G.DrawImage(B, R)
		End If
	End Sub

End Class
